package site.shenjie.article.controller;

import com.alibaba.fastjson.JSONObject;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

@RestController
public class ArticleController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    @Qualifier("eurekaClient") // Autowired注入爆红：因为这个接口有多个实现类，使用@Qualifier可以指定使用哪个实现类
    // 可以获取默认元数据加自定义的附加元数据信息
    private EurekaClient eurekaClient;

    @Autowired
    // 只能获取默认元数据信息
    private DiscoveryClient discoveryClient;

    @RequestMapping("/article/callHello")
    public String callHello() {
        return restTemplate.getForObject("http://127.0.0.1:8880/user/hello", String.class);
    }

    @RequestMapping("/article/callHello2")
    public String callHello2() {
        // 只能获取默认元数据信息
        System.out.println("以方式一打印自身服务元数据信息：" + eurekaClient.getInstancesByVipAddress("eureka-client-article-service", false));
        System.out.println("以方式一打印其他服务元数据信息：" + eurekaClient.getInstancesByVipAddress("eureka-client-user-service", false));
        // 可以获取默认元数据加自定义的附加元数据信息
        System.out.println("以方式二打印自身服务元数据信息：" + JSONObject.toJSONString(discoveryClient.getInstances("eureka-client-article-service")));
        System.out.println("以方式二打印其他服务元数据信息：" + JSONObject.toJSONString(discoveryClient.getInstances("eureka-client-user-service")));

        return restTemplate.getForObject("http://eureka-client-user-service/user/hello", String.class);
    }
}
